function HTTPCore () { }

HTTPCore.prototype =
{
  response:null,
  status:null,
  tok: null,

  auth: function(user, pass) {
    var tok = user + ':' + pass;
    var hash = Base64.encode(tok);
    this.tok = "Basic " + hash;
  },

  doOperation: function (req)
  {
    p = new XMLHttpRequest;
    p.onload = null;

    p.open(req.getRequestMethod(),
            req.getRequestUri(),
            false);

    var s = req.getBody(p);
    req.setRequestHeaders(p);
    if(this.tok != null){
      p.setRequestHeader('Authorization', this.tok);
    }
    p.send(s);

    // since this is sync request, we get results after send()
    this.status = p.status + "";

    if ( this.status.substr(0,1) != "2" ) {
      return false;
    } else {
      this.response = p.responseText;
      this.responseXML = p.responseXML;
      this.xmlHttp = p;
      return true;
    }
  }
}

function customRequest(method,uri,body){
  this.uri = uri;
  this.method = method;
  this.headers = new Array();
  this.body = body;
}
customRequest.prototype.addRequestHeader = function (name,value){
  this.headers.push(new Array(name, value));
}

customRequest.prototype.setRequestHeaders = function (p)
{
  for(var i=0; i< this.headers.length; i++){
    p.setRequestHeader(this.headers[i][0],this.headers[i][1]);
  }
}
customRequest.prototype.getRequestUri = function (){
  return this.uri;
}
customRequest.prototype.getRequestMethod = function (){
  return this.method;
}
customRequest.prototype.getBody = function (p){
  return this.body;
}

function GetRequest(baseuri)
{
  this.baseu = baseuri;
  this.parameters = new Dictionary();
}

GetRequest.prototype =
{
  baseu: null,
  method: "GET",
  parameters: null,
  cnttype: null,
  cntenc: null,

  put: function (key,value)
  {
    this.parameters.put(key,value);
    return this;
  },

  getRequestUri: function ()
  {
    var uri = "";
    uri += this.baseu + "?";
    this.parameters.resetIterator();
    while (this.parameters.hasMoreElements())
    {
      var param = this.parameters.next();
      uri+= escape(param.key)+"="+escape(param.value)+"&";
    }
    uri = uri.slice(0,-1) //discard & or ?
    return uri;
  },

  getRequestMethod: function () { return this.method; },

  setRequestHeaders: function (p) { return null; },

  getBody: function () { return null; }
}

function PostRequest(baseuri)
{
  this.baseu = baseuri;
  this.parameters = new Dictionary();
}

PostRequest.prototype =
{
  baseu: null,
  method: "POST",
  parameters: null,
  cnttype: null,
  cntenc: null,

  put: function (key,value)
  {
    this.parameters.put(key,value);

    return this;
  },

  getRequestUri: function ()
  {
    var uri = "";
    uri += this.baseu;

    return uri;
  },

  getRequestMethod: function () { return this.method; },

  setRequestHeaders: function (p)
  {
    p.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  },

  getBody: function ()
  {
    var uri = "";
    this.parameters.resetIterator();
    while (this.parameters.hasMoreElements())
    {
      var param = this.parameters.next();
      if (this.parameters.hasMoreElements()) {
        uri+= escape(param.key)+"="+escape(param.value)+"&";
      } else {
        uri+= escape(param.key)+"="+escape(param.value);
        break;
      }
    }

    return uri;
  }
}

function PutRequest(baseuri)
{
  this.baseu = baseuri;
}

PutRequest.prototype =
{
  baseu: null,
  method: "PUT",
  parameters: null,
	body: null,
  cnttype: null,
  cntenc: null,

  setBody: function (value)
  {
    this.body = value;
  },

  getRequestUri: function ()
  {
    var uri = "";
    uri += this.baseu;

    return uri;
  },

  getRequestMethod: function () { return this.method; },

  setRequestHeaders: function (p)
  {
    p.setRequestHeader("Content-type","application/xml");
  },

  getBody: function ()
  {
    return this.body;
  }
}

function DeleteRequest(baseuri){
	this.baseu = baseuri;
}

DeleteRequest.prototype =
{
	getRequestUri: function ()
  {
    return this.baseu;
  },
	getRequestMethod: function () { return "DELETE"; },

	setRequestHeaders: function (p) { return null; },

	getBody: function ()
  {
    return null;
  }
}